package cn.xiaoliu.product.service.impl;

import cn.xiaoliu.basic.service.impl.BaseServiceImpl;
import cn.xiaoliu.org.domain.Employee;
import cn.xiaoliu.org.mapper.EmployeeMapper;
import cn.xiaoliu.product.domain.Product;
import cn.xiaoliu.product.domain.ProductComment;
import cn.xiaoliu.product.domain.ProductDetail;
import cn.xiaoliu.product.mapper.ProductCommentMapper;
import cn.xiaoliu.product.mapper.ProductDetailMapper;
import cn.xiaoliu.product.mapper.ProductMapper;
import cn.xiaoliu.product.service.IProductService;
import cn.xiaoliu.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductCommentMapper productCommentMapper;


    //新增服务
    @Override
    public void submit(Product product, Logininfo logininfo) {
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id()!=null){
            product.setShop_id(employee.getShop_id());
        }
        ProductDetail detail = product.getDetail();
        productMapper.save(product);
        if (detail != null){//详情不为空才保存
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }
    //服务上架
    @Override
    public void onsale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onSaleTime",new Date());
        productMapper.onSale(map);
    }
     //服务下架
    @Override
    public void offsale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offSaleTime",new Date());
        productMapper.offSale(map);
    }

    //查询宠物的详情
    @Override
    public Product queryByIdDetail(Long id) {

        return productMapper.loadByIdDetail(id);
    }


    //删除服务
    @Override
    public void remove(Long id) {
        ProductDetail detail = productDetailMapper.loadByProductId(id);
        if (detail!=null){
            productDetailMapper.deleteByProductId(id);
        }
        ProductComment comment = productCommentMapper.loadByProductId(id);
        if (comment!=null){
            productCommentMapper.deleteByProductId(id);
        }
        productMapper.delete(id);
    }
}
